package com.example.Project.user.userHttpMethods;

import com.example.Project.Class.Apply;
import com.example.Project.Class.MassOrganizations;
import com.example.Project.Class.User;
import com.example.Project.Class.queryMass;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.gson.JsonObject;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class userMethods {
    //分页查询
    public ObservableList<MassOrganizations> getMassOrganization(int page) throws IOException {
        String serverAddress = "http://localhost:5555/api/getMassOrganizations?page="+page;
        System.out.println("ppppp:"+page);

        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<MassOrganizations> massOrganizationsList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<MassOrganizations>>() {
                        })
                );
                return massOrganizationsList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //删除数据
    public boolean deleteMassOrganization(int id) throws IOException {
        String serverAddress = "http://localhost:5555/api/deleteMassOrganization?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("POST");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("POST 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //修改某条数据
    public boolean modifyMassOrganization(MassOrganizations mass) throws IOException {
        String serverAddress = "http://localhost:5555/api/modifyMassOrganization";
        try {

            JsonObject jsonData = mass.toJsonObject();
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("POST");// 设置请求方法为 GET
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            try (OutputStream os = connection.getOutputStream();
                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
                writer.write(jsonData.toString());
                writer.flush();
            }
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);


            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("POST 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //获取总页数
    public int getTotalPage(){
        String serverAddress = "http://localhost:5555/api/getTotalPage";
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                int result = objectMapper.readValue(reader, Integer.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("GET 请求失败");
                return 1;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //模糊查询
    public ObservableList<MassOrganizations> queryMassOrganization(queryMass query) throws IOException {
        System.out.println(query.getId()+":"+query.getName());
        String serverAddress = "http://localhost:5555/api/getQueryMass";

        try {
            JsonObject jsonData = query.toJsonObject();
            System.out.println(jsonData);
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            try (OutputStream os = connection.getOutputStream();
                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
                writer.write(jsonData.toString());
                writer.flush();
            }
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<MassOrganizations> massOrganizationsList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<MassOrganizations>>() {
                        })
                );
                return massOrganizationsList;
            } else {
                System.out.println("POST 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean addMassOrganization(MassOrganizations mass){
        String serverAddress = "http://localhost:5555/api/addMassOrganization";
        try {
            JsonObject jsonData = mass.toJsonObject();
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("POST");// 设置请求方法为 GET
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            try (OutputStream os = connection.getOutputStream();
                 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
                writer.write(jsonData.toString());
                writer.flush();
            }
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);


            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader= new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("POST 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //登录
    public boolean login(String identity,String account,String password) throws IOException {
        identity = URLEncoder.encode(identity, StandardCharsets.UTF_8);
        account = URLEncoder.encode(account, StandardCharsets.UTF_8);
        password = URLEncoder.encode(password, StandardCharsets.UTF_8);
        String serverAddress = "http://localhost:5555/api/login?identity="+ identity + "&account=" + account + "&password=" + password;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("GET 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //获取用户信息
    public User getUserInfo(int id) {
        String serverAddress = "http://localhost:5555/api/getUserInfo?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                User response = objectMapper.readValue(reader, User.class);
                System.out.println(response.getId()+response.getName());
                return response;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ObservableList<MassOrganizations> getUserMass(int id) throws IOException {
        System.out.println(id);
        String serverAddress = "http://localhost:5555/api/getUserMass?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<MassOrganizations> massOrganizationsList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<MassOrganizations>>() {
                        })
                );
                return massOrganizationsList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ObservableList<User> getMassUser(int id) throws IOException {
        System.out.println(id);
        String serverAddress = "http://localhost:5555/api/getMassUser?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<User> usersList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<User>>() {
                        })
                );
                return usersList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean userSendAdd(int userid,int massid){
        userid = Integer.parseInt(URLEncoder.encode(String.valueOf(userid), StandardCharsets.UTF_8));
        massid = Integer.parseInt(URLEncoder.encode(String.valueOf(massid), StandardCharsets.UTF_8));
        String serverAddress = "http://localhost:5555/api/userSendAdd?userid="+userid+"&massid="+massid;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("POST 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean userSendDel(int userid,int massid,int i){
        userid = Integer.parseInt(URLEncoder.encode(String.valueOf(userid), StandardCharsets.UTF_8));
        massid = Integer.parseInt(URLEncoder.encode(String.valueOf(massid), StandardCharsets.UTF_8));
        i = Integer.parseInt(URLEncoder.encode(String.valueOf(i), StandardCharsets.UTF_8));
        String serverAddress = "http://localhost:5555/api/userSendDel?userid="+userid+"&massid="+massid+"&i="+i;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("GET 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ObservableList<MassOrganizations> getMasterMass(int id) throws IOException {
        String serverAddress = "http://localhost:5555/api/getMasterMass?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<MassOrganizations> massOrganizationsList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<MassOrganizations>>() {
                        })
                );
                return massOrganizationsList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ObservableList<Apply> getDelApply(int id) throws IOException {
        System.out.println(id);
        String serverAddress = "http://localhost:5555/api/getDelApply?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<Apply> applyList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<Apply>>() {
                        })
                );
                return applyList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ObservableList<Apply> getAddApply(int id) throws IOException {
        System.out.println(id);
        String serverAddress = "http://localhost:5555/api/getAddApply?id="+id;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());

                ObservableList<Apply> applyList = FXCollections.observableArrayList(
                        objectMapper.readValue(reader, new TypeReference<List<Apply>>() {
                        })
                );
                return applyList;
            } else {
                System.out.println("GET 请求失败");
                return null;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean deleteMassUser(int userid,int massid){
        userid = Integer.parseInt(URLEncoder.encode(String.valueOf(userid), StandardCharsets.UTF_8));
        massid = Integer.parseInt(URLEncoder.encode(String.valueOf(massid), StandardCharsets.UTF_8));
        String serverAddress = "http://localhost:5555/api/deleteMassUser?userid="+userid+"&massid="+massid;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                boolean result = objectMapper.readValue(reader, Boolean.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("GET 请求失败");
                return false;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public int register(String name,String pass,String sex,String phone) throws IOException {
        name = URLEncoder.encode(name, StandardCharsets.UTF_8);
        sex = URLEncoder.encode(sex, StandardCharsets.UTF_8);
        pass = URLEncoder.encode(pass, StandardCharsets.UTF_8);
        phone=URLEncoder.encode(phone,StandardCharsets.UTF_8);
        String serverAddress = "http://localhost:5555/api/register?name="+ name + "&pass=" + pass + "&sex=" + sex+ "&phone=" + phone;
        try {
            URL url = new URL(serverAddress);// 创建 URL 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开与 URL 的连接
            connection.setRequestMethod("GET");// 设置请求方法为 GET
            int responseCode = connection.getResponseCode();// 获取响应码
            System.out.println("响应码: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                ObjectMapper objectMapper = new ObjectMapper();
                objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
                objectMapper.registerModule(new JavaTimeModule());
                Integer result = objectMapper.readValue(reader, int.class);
                System.out.println(result);
                return result;
            } else {
                System.out.println("GET 请求失败");
                return -1;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
